package com.hm.pictureselect.slice;

import com.hm.pictureselect.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.Image;

public class MainAbilitySlice extends AbilitySlice {
    private Image image;
    private Button button;
    private int requestCode = 123;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        initLayout();
    }

    private void initLayout() {
        image = (Image)findComponentById(ResourceTable.Id_image_view);
        button = (Button)findComponentById(ResourceTable.Id_button_picture_select);
        button.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                Intent intent_picture_select = new Intent();
                presentForResult(new PictureSelectAbilitySlice(), intent_picture_select, requestCode);
            }
        });
    }

    @Override
    protected void onResult(int requestCode, Intent resultIntent) {
        super.onResult(requestCode, resultIntent);
        if (this.requestCode == requestCode){
            int res_id = resultIntent.getIntParam("picture_res_id", ResourceTable.Media_icon);
            image.setImageAndDecodeBounds(res_id);
        }
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
